package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.model.common.app.pojo.AppUser;
import com.zhentao.model.common.app.dto.UserLoginDto;
import com.zhentao.service.AppUserService;
import com.zhentao.mapper.AppUserMapper;
import com.zhentao.util.AppThreadLocalUtil;
import com.zhentao.utils.common.AppJwtUtil;
import com.zhentao.utils.common.MD5Utils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;

/**
* @author 贾媛婕
* @description 针对表【app_user】的数据库操作Service实现
* @createDate 2024-09-14 09:40:49
*/
@Service
public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser>
    implements AppUserService{

    @Override
    public ResponseResult userLogin(UserLoginDto dto) {
        System.out.println(dto);
        //校验参数
        boolean empty = ObjectUtils.isEmpty(dto);
        if (empty){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"手机号或密码为空");
        }

        System.err.println(dto.getPhone());
        if (dto.getPhone().length() != 11){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"手机号格式不正确");
        }

        String phone = dto.getPhone();
        AppUser one = this.getOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getPhone, phone));

        String s = MD5Utils.encodeWithSalt(dto.getPassword(), one.getSalt());
        if(!s.equals(one.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR,"密码错误");
        }

        String token = AppJwtUtil.getToken(Long.getLong(one.getId() + ""));
        one.setPassword("");
        one.setSalt("");
        Map<String,Object> map = new HashMap<>();
        map.put("msg","登陆成功");
        map.put("token",token);
        map.put("user",one);

        return ResponseResult.okResult(map);
    }

    @Override
    public ResponseResult register(UserLoginDto dto) {
        //校验参数
        boolean empty = ObjectUtils.isEmpty(dto);
        if (empty){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"手机号或密码为空");
        }

        //判断手机号是否存在
        AppUser one = this.getOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getPhone, dto.getPhone()));
        if (one != null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"手机号已被注册");
        }

        AppUser appUser = new AppUser();
        BeanUtils.copyProperties(dto,appUser);
        String salt = String.valueOf(UUID.randomUUID()).substring(0, 5);
        appUser.setSalt(salt);

        String password = MD5Utils.encodeWithSalt(dto.getPassword(), salt);
        appUser.setPassword(password);
        appUser.setCreateTime(new Date());

        System.out.println(dto);
        //添加
        boolean save = this.save(appUser);


        return save?ResponseResult.okResult(200,"注册成功"):ResponseResult.errorResult(500,"注册失败");
    }

    @Override
    public ResponseResult userList() {
        List<AppUser> list = this.list();
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult userInfo(AppUser user) {

        boolean empty = ObjectUtils.isEmpty(user);
        if (empty){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"有值为空，请检查数据");
        }

        AppUser user1 = AppThreadLocalUtil.getUser();
        Integer id = user1.getId();

        AppUser one = this.getById(id);
        BeanUtils.copyProperties(user,one);
        one.setName(user.getName());
        one.setAge(user.getAge());
        one.setSex(one.getSex());
        one.setAvatar(user.getAvatar());

        this.updateById(one);


        return ResponseResult.okResult(200);
    }

    @Override
    public ResponseResult getUserById(Integer id) {
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"id不存在");
        }
        AppUser byId = this.getById(id);
        if(byId == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"id不存在");
        }

        return ResponseResult.okResult(byId);
    }


}




